package com.codejam.mbg;

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhaosen
 * @date 3/9/2020
 */
public class Main {

    public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException {
        try {
            List<String> warnings = new ArrayList<>();

            boolean overwrite = true;

            String generatorConfig = "/generatorConfig.xml";
            File configFile = new File(Main.class.getResource(generatorConfig).getFile());

            ConfigurationParser configurationParser = new ConfigurationParser(warnings);
            Configuration configuration = null;

            try {
                configuration = configurationParser.parseConfiguration(configFile);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XMLParserException e) {
                e.printStackTrace();
            }


            DefaultShellCallback callback = new DefaultShellCallback(overwrite);

            MyBatisGenerator myBatisGenerator = null;
            try {
                myBatisGenerator = new MyBatisGenerator(configuration, callback, warnings);
            }
            catch (InvalidConfigurationException e){
                e.printStackTrace();
            }

            myBatisGenerator.generate(null);

        } catch (Throwable e) {
            e.printStackTrace();
        }


    }
}
